package com.phynos.web.manager.ui.component;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * 专门针对adminlte菜单栏的数据结构
 * @author lupc
 *
 */
public class UISidebarMenu {

	/**
	 * 当前激活的item
	 */
	@JsonIgnore
	private UISidebarMenuItem current;
	
	private List<UISidebarMenuItem> menus;
	
	/**
	 * 设置 当前选择菜单
	 * @param f1
	 * @param f2
	 */
	public void setActive(int f1,int f2){
		for (int i = 0; i < menus.size(); i++) {			
			UISidebarMenuItem menu = menus.get(i);
			if(i == f1){
				current = menu;
				menu.setAcitve(true);
			} else {
				menu.setAcitve(false);
			}			
			if(!menu.hasChildren()){
				continue;
			}
			for (int j = 0; j < menu.items.size(); j++) {
				UISidebarMenuItem item = menu.items.get(j);
				if(i == f1 && j == f2){
					item.setAcitve(true);
					current = item;
				} else {
					item.setAcitve(false);
				}
			}
		}
	}
	
	/**
	 * 根据id来设置active
	 * @param id
	 */
	public void setActive(String id){
		for (int i = 0; i < menus.size(); i++) {			
			UISidebarMenuItem menu = menus.get(i);
			//
			if(!menu.hasChildren()){
				String pid = menu.getId() == null? "":menu.getId();
				if(pid.equals(id)){
					current = menu;
					menu.setAcitve(true);
				} else {
					menu.setAcitve(false);
				}				
				continue;
			}
			boolean activeParent = false;
			for (int j = 0; j < menu.items.size(); j++) {
				UISidebarMenuItem item = menu.items.get(j);
				String _id = item.getId();
				if(_id != null && _id.equals(id)){
					activeParent = true;
					item.setAcitve(true);
					current = item;
				} else {
					item.setAcitve(false);
				}
			}
			menu.setAcitve(activeParent);
		}
	}
	
	public static class UISidebarMenuItem {
		
		/**
		 * 唯一识别
		 */
		private String id;
		
		/**
		 * 显示文本
		 */
		private String text;
		/**
		 * 描述信息
		 */
		private String describe;
		/**
		 * 当前URL（仅当没有子菜单才有效）
		 */
		private String url;
		/**
		 * 图标css名
		 */
		private String iconCss;
		/**
		 * 是否是当前选中
		 */
		@JsonProperty("isAcitve")
		private boolean isAcitve;
				
		private List<UISidebarMenuItem> items;

		public UISidebarMenuItem(){
			isAcitve = false;
		}
		
		public String getText() {
			return text;
		}

		public void setText(String text) {
			this.text = text;
		}

		/**
		 * 是否有子级菜单
		 * @return
		 */
		public boolean hasChildren() {
			if(items == null || items.size() < 1)
				return false;
			else 
				return true;
		}

		public String getUrl() {
			return url;
		}

		public void setUrl(String url) {
			this.url = url;
		}

		public String getIconCss() {
			return iconCss;
		}

		public void setIconCss(String iconCss) {
			this.iconCss = iconCss;
		}

		public boolean isAcitve() {
			return isAcitve;
		}

		public void setAcitve(boolean isAcitve) {
			this.isAcitve = isAcitve;
		}

		public List<UISidebarMenuItem> getItems() {
			return items;
		}

		public void setItems(List<UISidebarMenuItem> items) {
			this.items = items;
		}

		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		public String getDescribe() {
			return describe;
		}

		public void setDescribe(String describe) {
			this.describe = describe;
		}
				
	}


	public List<UISidebarMenuItem> getMenus() {
		return menus;
	}


	public void setMenus(List<UISidebarMenuItem> menus) {
		this.menus = menus;
	}

	public UISidebarMenuItem getCurrent() {
		return current;
	}
	
}
